package com.codeoss.framework.view.renderer;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.converter.IConverter;
import com.codeoss.framework.converter.IConverterStore;
import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.TableData;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.mobile.MListView;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.model.SelectItem;

@Service("mlistviewRenderer")
public class MListViewRenderer extends CommonComponentRenderer {
	@Autowired
	private IConverterStore converterStore;
	@Autowired
	private ITableStore tableStore;
	protected void startRenderView(YunContext yunContext, Component component, ViewSerializeMap viewSerializeMap) {
		super.startRenderView(yunContext, component, viewSerializeMap);
		MListView mListView = (MListView) component;
		viewSerializeMap.put("editable", mListView.isEditable());
		viewSerializeMap.put("formPageCode", mListView.getFormPageCode());
		viewSerializeMap.put("pageable", mListView.isPageable());
		viewSerializeMap.put("pageSize", mListView.getPageSize());
		
		String dataSource=mListView.getDataSource();
		if(!StringUtils.isEmpty(dataSource)) {
			DataSourceConfig dataSourceConfig=yunContext.getPage().getDataSourceConfigById(dataSource);
			com.codeoss.framework.data.metadata.Table ttable=tableStore.get(dataSourceConfig.getTableName());
			viewSerializeMap.put("keyColumnName", ttable.getPrimaryKeyColumn().getColumnName());
		}
	}
	
	protected PropSerializeMap startRenderProp(YunContext yunContext, Component component, ViewPropMap viewPropMap) {
		PropSerializeMap propSerializeMap = super.startRenderProp(yunContext, component, viewPropMap);
		MListView mListView = (MListView) component;

		TableData tableData=(TableData)yunContext.getAttribute(mListView.getId()+"_tabledata");
		Map<String,List<SelectItem>> selectItemMap=yunContext.getAttribute(mListView.getId()+"_selectItemMap");
		
		if(tableData==null) {
			propSerializeMap.put("tableData", null);
			propSerializeMap.put("selectItemMap", null);
			return propSerializeMap;
		}
		IConverter datetimeConverter=converterStore.getConverter("datetime");
		IConverter dateConverter=converterStore.getConverter("date");
		IConverter timeConverter=converterStore.getConverter("time");
		IConverter booleanConverter=converterStore.getConverter("boolean");
		
		Page page=yunContext.getPage();
		DataSourceConfig dataSourceConfig=page.getDataSourceConfigById(mListView.getDataSource());
		com.codeoss.framework.data.metadata.Table table=tableStore.get(dataSourceConfig.getTableName());
		
		List<Map<String,Object>> dataList=tableData.getData();
		for(int i=0;i<dataList.size();i++) {
			Map<String,Object> map=dataList.get(i);
			Iterator<Map.Entry<String, Object>> it=map.entrySet().iterator();
			while(it.hasNext()) {
				Map.Entry<String, Object> entry=it.next();
				Object value=entry.getValue();
				if(value!=null) {
					if(value instanceof Timestamp) {
						String sValue=datetimeConverter.convertObjectToString(value);
						map.put(entry.getKey(), sValue);
					}else if(value instanceof Date) {
						String sValue=dateConverter.convertObjectToString(value);
						map.put(entry.getKey(), sValue);
					}else if(value instanceof Time) {
						String sValue=timeConverter.convertObjectToString(value);
						map.put(entry.getKey(), sValue);
					}else {
						String columnName=entry.getKey();
						Column column=table.getColumnByColumnName(columnName);
						if(column.getDataType().equals("boolean") && !(value instanceof Boolean)) {
							map.put(entry.getKey(), booleanConverter.convertStringToObject((String)value));
						}
					}
				}
			}
		}
		
		propSerializeMap.put("tableData", tableData);
		propSerializeMap.put("selectItemMap", selectItemMap);

		return propSerializeMap;
	}
}
